from habanero import Crossref

# 使用 API 密钥 (推荐)
cr = Crossref(mailto="934096384@qq.com")


def test1():
  doi = "10.1056/nejmp058318"
  article_data = cr.works(ids=doi)

  # 打印文章标题
  print(article_data['message']['title'][0])

def qeury_by_title_author_container(title, author, container_title):
  query_cont = f"""title:"{title}" AND author:"{author}" and container-title:"{container_title}"  """
  results = cr.works(query=query_cont, limit=3) # 会自动按相关性排序，返回前 10 篇文章
  rtn_list = []
  for item in results['message']['items']:
    rtn_list.append(item)
  return rtn_list


  # 获取标题信息
  title = parsed_data.entries.values()[0].fields['title']

  # 获取期刊名称
  container_title = parsed_data.entries.values()[0].fields['journal']

  print(f"作者: {author}")
  print(f"标题: {title}")
  print(f"期刊: {container_title}")

def test2():
  title = "Chronic kidney disease in the developing"  # 替换为目标论文的标题
  author = "Barsoum RS."  # 替换为目标作者的姓名
  container_title="N Engl J Med"

  results = qeury_by_title_author_container(title, author, container_title)
  for item in results:
    print(f"title = {item['title'][0]}  , doi = {item['DOI']}")  # 打印文章标题

def test3():
  refs_to_search = [
    ['Chronic kidney disease in the developing','Barsoum RS.','N Engl J Med',''], # tile, author, container_title
    ['Prevalence and control of diabetes in Chinese adults','Xu Y, Wang L, He J, et al.','JAMA'],
    ['Prevalence of diabetes among men and women in China','Yang W, Lu J, Weng J, et al','N Engl J Med'],
    ['Prevalence of diabetes and its risk factors in China','Pan XR, Yang WY, Li GW, Liu J','Diabetes Care'],
  ]
  # title = "Prevalence and control of diabetes in Chinese adults"  # 替换为目标论文的标题
  # author = "Xu Y, Wang L, He J, et al."  # 替换为目标作者的姓名
  # container_title="JAMA"

  for title, author, container_title in refs_to_search:
    results = qeury_by_title_author_container(title, author, container_title)
    for item in results:
      author_list = item['author'] if 'author' in item else []
      score = item['score'] if 'score' in item else 0
      print(f"title = {item['title'][0]}  , doi = {item['DOI']}  , author = {author_list} , score = {score} ")  # 打印文章标题

if __name__ == '__main__':
  # get_author_title_container()
  test3()

"""
title = Chronic Kidney Disease in the Developing World  , doi = 10.1056/nejmp058318
title = Prevalence and Control of Diabetes in Chinese Adults  , doi = 10.1001/jama.2013.168118
title = Prevalence of Diabetes among Men and Women in China  , doi = 10.1056/nejmc1004671  --》 10.1056/NEJMoa0908292
title = Prevalence of Diabetes and Its Risk Factors in China, 1994  , doi = 10.2337/diacare.20.11.1664

"""